package com.s3.khospitalds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.s3.khospitalds.common.ResponseEntity;
import com.s3.khospitalds.entity.Doctor;
import com.s3.khospitalds.entity.DoctorResignInfo;
import com.s3.khospitalds.mapper.DoctorMapper;
import com.s3.khospitalds.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @Author: 想要告诉你
 * @CreateTime: 2025/2/19 10:38
 * @Description:
 * @Version: 1.0
 */

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private DoctorServiceImpl doctorService;

    @Override
    public List<Doctor> getDoctorResignInfo() {
        return doctorMapper.getDoctorResignInfo();
    }

    // 查询数据
    @Override
    public ResponseEntity getDoctor(int pageNum, int pageSize, String search) {
        ResponseEntity responseEntity = new ResponseEntity();
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //模糊查询
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",search);
        queryWrapper.eq("isexpert",1);
        //使用分页查询
        List<Doctor> doctors = doctorService.list(queryWrapper);
        //创建分页的对象
        PageInfo<Doctor> pageInfo = new PageInfo<>(doctors);
        //设置响应对象的值
        responseEntity.setCount((int) pageInfo.getTotal());
        //设置响应对象的行数据
        responseEntity.setData(pageInfo.getList());
        return responseEntity;
    }

    // 获取已离职的医生
    @Override
    public ResponseEntity getResignDoctor() {
        ResponseEntity responseEntity = new ResponseEntity();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isexpert",2);
        List<Doctor> doctors = doctorService.list(queryWrapper);
        responseEntity.setData(doctors);
        return responseEntity;
    }

    // 插入数据
    @Override
    public int insertDoctor(Doctor doctor) {
        return doctorMapper.insertDoctor(doctor);
    }

    // 修改数据
    @Override
    public boolean updateDoctor(Doctor doctor) {
        // 获取 doctor 的 date 字段
        Date date = doctor.getDate();

        // 将 Date 转换为 LocalDate
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        // 增加一天
        LocalDate newLocalDate = localDate.plusDays(1);

        // 将 LocalDate 转换回 Date
        Date newDate = Date.from(newLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        // 更新 doctor 对象的 date 字段
        doctor.setDate(newDate);

        // 调用 mapper 更新医生信息
        return doctorMapper.updateDoctor(doctor);
    }

    // 删除数据
    @Override
    public boolean deleteDoctor(DoctorResignInfo doctorResignInfo) {
        //return doctorMapper.deleteDoctor(id);
        return doctorMapper.updateDoctorResign(doctorResignInfo);
    }
}
